package com.gzyj.server.factory.jtt808.down;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.server.config.LogConfig;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.factory.jtt808.NetWorkSerrvice;
import com.gzyj.server.utils.StackUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;

@Component
public class JTT808_0x9205_Handler implements CommonHandler {
    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }

    @Autowired
    private NetWorkSerrvice netWorkSerrvice;


    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {
        JSONObject command = JSON.parseObject(msg);
        String ipaddress = command.getString("ipAddress");
        String cmdId = command.getString("cmdID");
        Byte channel = command.getByte("channel");

        String beginTime = command.getString("beginTime");
        String endTime = command.getString("endTime");
        Long alarm = command.getLong("alarm");
        byte resourceType = command.getByte("resourceType");


        Byte bitstreamType = command.getByte("bitstreamType");
        byte storageType = command.getByte("storageType");

        Integer seq = command.getIntValue("seq");



        if (StringUtils.isNotEmpty(vehicleInfo.getTerminalprotocoltype()) && vehicleInfo.getTerminalprotocoltype().equalsIgnoreCase("2019")) {


        } else {
            com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x9205 jtt808_0x9205 = new com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x9205();
            com.gzyj.jtt808.v2013.HeaderMsgBodyProperty headerMsgBodyProperty = new com.gzyj.jtt808.v2013.HeaderMsgBodyProperty();

            headerMsgBodyProperty.setSubcontract(false);
            headerMsgBodyProperty.setEncryptMethod(com.gzyj.jtt808.v2013.HeaderEncryptMethod.None);
            jtt808_0x9205.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
            jtt808_0x9205.getPackageHeader().setTerminalPhoneNo(ipaddress);
            jtt808_0x9205.getPackageHeader().setMsgNum(seq);

            jtt808_0x9205.setAlarmFlag(alarm);
            jtt808_0x9205.setMediaType(resourceType);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            try {
                jtt808_0x9205.setBeginTime(sdf.parse(beginTime));
                jtt808_0x9205.setEndTime(sdf.parse(endTime));
            } catch (ParseException e) {
                LogConfig.commondErrorLog.error("808 2013版 9205 下发 查询资源列表 指令错误 \n {}", StackUtils.getStackInfo(e));
            }

            jtt808_0x9205.setChannelNo(channel);
            jtt808_0x9205.setMemoryType(storageType);
            jtt808_0x9205.setStreamType(bitstreamType);

            ByteBuf data = jtt808_0x9205.encode();
            netWorkSerrvice.send2Veh(ipaddress, data);
            LogConfig.commondLog.info("808 2013版 9205 下发 查询资源列表 指令 {} {}", ipaddress, ByteBufUtil.hexDump(data));

        }
    }
}
